package com.cw.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cw.common.api.ResultObject;
import com.cw.dto.*;
import com.cw.request.VYqBrdjxxConditionRequest;
import com.cw.service.IHisCwglDeptService;
import com.cw.service.IThisIiPrepayinService;
import com.cw.service.IVYqBrdjxxService;
import com.cw.util.PageHelperTool;
import com.cw.util.StringUtil;
import com.cw.vo.PatientInfoToDoctorVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.io.IOException;
import java.util.*;

@Api(tags = {"院前准备接口"})
@RequestMapping("/VYqBrdjxxController")
@RestController
public class VYqBrdjxxController {

    private IVYqBrdjxxService ivYqBrdjxxService;

    private IHisCwglDeptService hisCwglDeptService;

    private IThisIiPrepayinService iThisIiPrepayinService;

    @Autowired
    private void setterAutoWired(IVYqBrdjxxService ivYqBrdjxxService,
                                 IHisCwglDeptService hisCwglDeptService,
                                 IThisIiPrepayinService iThisIiPrepayinService) {
        this.ivYqBrdjxxService = ivYqBrdjxxService;
        this.hisCwglDeptService = hisCwglDeptService;
        this.iThisIiPrepayinService = iThisIiPrepayinService;
    }

    @ApiOperation("院前准备列表")
    @PostMapping("/getVYqBrdjxxPagesInfo")
    public ResultObject getVYqBrdjxxPagesInfo(@RequestBody VYqBrdjxxCondition condition) {
        return ResultObject.successReturn(ivYqBrdjxxService.getVYqBrdjxxPagesInfo(condition));
    }

//    @ApiOperation("日间手术列表")
//    @PostMapping("/getVYqBrdjxxRJSSPagesInfo")
//    public ResultObject getVYqBrdjxxRJSSPagesInfo(@RequestBody VYqBrdjxxCondition condition) {
//        return ResultObject.successReturn(ivYqBrdjxxService.getVYqBrdjxxRJSSPagesInfo(condition));
//    }

    @ApiOperation("院前准备列表下载")
    @PostMapping("/getVYqBrdjxxDownLoad")
    public void getVYqBrdjxxDownLoad(@RequestBody VYqBrdjxxCondition condition){
        ivYqBrdjxxService.getVYqBrdjxxDownLoad(condition);
    }

//    @ApiOperation("日间手术列表下载")
//    @PostMapping("/getVYqBrdjxxRJSSDownLoad")
//    public void getVYqBrdjxxRJSSDownLoad(@RequestBody VYqBrdjxxCondition condition){
//        ivYqBrdjxxService.getVYqBrdjxxRJSSDownLoad(condition);
//    }


    @ApiOperation("已登记未登记列表")
    @PostMapping("/gePpeopleStatistics")
    public ResultObject gePpeopleStatistics(@RequestBody VYqBrdjxxCondition condition) {
        return ResultObject.successReturn(ivYqBrdjxxService.gePpeopleStatistics(condition));
    }



    @ApiOperation("获取科室信息")
    @GetMapping("/getDept")
    public ResultObject getDeptsGroup() {
        return ResultObject.successReturn(hisCwglDeptService.getHisCwglDeptByUserAccess(null));
    }

    @ApiOperation("院前准备详情")
    @PostMapping("/getVYqBrdjxx")
    public ResultObject getVYqBrdjxx(@RequestBody VyqBrdjxxVeiwCondition condition) {
        return ResultObject.successReturn(ivYqBrdjxxService.getVYqBrdjxx(condition));
    }

//    @ApiOperation("日间手术详情")
//    @PostMapping("/getRJSSVYqBrdjxx")
//    public ResultObject getRJSSVYqBrdjxx(@RequestBody VyqBrdjxxVeiwCondition condition) {
//        return ResultObject.successReturn(ivYqBrdjxxService.getRJSSVYqBrdjxx(condition));
//    }


    @ApiOperation("院前准备报表")
    @PostMapping("/getReportList")
    public ResultObject getReportList(@RequestBody VYqBrdjxxConditionRequest requestInfo) {
        List<ReportReadyDocNameDTO> reportList = ivYqBrdjxxService.getReportList(requestInfo);
        PageInfo<ReportReadyDocNameDTO> reportReadyDocNameDTOPageInfo = PageHelperTool.excutePageInfo(reportList, requestInfo.getPageIndex(), requestInfo.getPageSize());
        return ResultObject.successReturn(reportReadyDocNameDTOPageInfo);
    }



    @ApiOperation("院前报表-医生所负责患者信息")
    @PostMapping("/listPatientInfoToDoctor")
    public ResultObject listPatientInfoToDoctor(@RequestBody PatientInfoToDoctorDTO requestInfo) {

        List<PatientInfoToDoctorVO> patientInfoToDoctorVOS = ivYqBrdjxxService.listPatientInfoToDoctor(requestInfo);

        PageInfo<PatientInfoToDoctorVO> resultList = PageHelperTool.excutePageInfo(patientInfoToDoctorVOS,
                requestInfo.getPageNum(),
                requestInfo.getPageSize());
        return ResultObject.success(resultList);
    }

    @ApiOperation("院前报表-医生所负责患者信息--excel下载")
    @PostMapping("/getListPatientInfoToDoctorExcel")
    public void getListPatientInfoToDoctorExcel(@RequestBody PatientInfoToDoctorDTO requestInfo) throws IOException {

        ivYqBrdjxxService.getListPatientInfoToDoctorExcel(requestInfo);
    }



    @ApiOperation("院前准备报表--excel下载")
    @PostMapping("/getReportListDownLoadExcel")
    public void getReportListDownLoadExcel(@RequestBody VYqBrdjxxConditionRequest requestInfo) throws IOException {
        ivYqBrdjxxService.getReportListDownLoadExcel(requestInfo);
    }


    /**
     * NEW
     * */
    @ApiOperation("一体化病历院前准备报表")
    @GetMapping("/getReportListIntegrated")
    public ResultObject getReportListIntegrated(String deptCodeList,String jobNumber) {
        return ResultObject.successReturn(ivYqBrdjxxService.getReportListIntegrated(deptCodeList,jobNumber));
    }

    @ApiOperation("一体化病历日间病房中心报表")
    @GetMapping("/getRJSSReportListIntegrated")
    public ResultObject getRJSSReportListIntegrated(String deptCodeList,String jobNumber) {
        return ResultObject.successReturn(ivYqBrdjxxService.getRJSSReportListIntegrated(deptCodeList,jobNumber));
    }


    @ApiOperation("一体化院前准备报表下载--excel下载")
    @GetMapping("/getReportListIntegratedDownLoadExcel")
    public void getReportListIntegratedDownLoadExcel(String deptCodeList,String jobNumber) throws IOException {
        ivYqBrdjxxService.getReportListIntegratedDownLoadExcel(deptCodeList,jobNumber);
    }

    @ApiOperation("一体化日间病房中心报表下载--excel下载")
    @GetMapping("/getRJSSReportListDownLoadExcel")
    public void getRJSSReportListDownLoadExcel(String deptCodeList,String jobNumber) throws IOException {
        VYqBrdjxxConditionRequest requestInfo =new VYqBrdjxxConditionRequest();
        requestInfo.setJobNumber(jobNumber);
        List<String> lstDept = Arrays.asList(deptCodeList.split(","));
        requestInfo.setDeptCodeList(lstDept);
        ivYqBrdjxxService.getRJSSReportListDownLoadExcel(requestInfo);
    }

    @ApiOperation("取消功能")
    @GetMapping("/cancelIiprepayIn")
    public ResultObject cancelIiprepayIn(@RequestParam String preId,String state) {
        return ResultObject.successReturn(iThisIiPrepayinService.cancelIiprepayIn(preId,state));
    }

    @ApiOperation("获取检验记录详情")
    @GetMapping("/getReportDetail")
    public ResultObject getReportDetail(@RequestParam String doctrequestno,String sampleNo,String patientNo) {
        Map<String, Object> map = new HashMap<>(4);
        map.put("RequestNo", doctrequestno);
        map.put("ReportNo", sampleNo);
        map.put("VisitNumber", patientNo);
        map.put("HospitalCode", "01");

        String param = "172.16.2.226:8041/PatientJson/get_ModalLisItem";

        List<InspectExamineDTO> inspectExamineDTO = new ArrayList<>();
        try {
            //开始请求360检验详情
            String post = HttpUtil.post(param ,map,9000);
            if (StringUtil.isEmpty(post)) { return ResultObject.errorReturn("已请求!但未返回任何参数!!"); }
            JSONArray jsonArray = JSON.parseArray(post);
            for (Object o : jsonArray) {
                inspectExamineDTO.add(JSON.parseObject(o.toString(), InspectExamineDTO.class));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultObject.successReturn(inspectExamineDTO);
    }

//    @ApiOperation("自己本系统的手工电子住院证")
//    @PostMapping("/getHandworkPrepayin")
//    public ResultObject getHandworkPrepayin(@RequestBody HandworkPrepayinDto handworkPrepayinDto){
//        return ResultObject.successReturn(ivYqBrdjxxService.getHandworkPrepayin(handworkPrepayinDto));
//    }


}
